const express=require("express")
const router=express.Router();
const pool=require("../pool");
//功能九：添加购物车
router.get("/",(req,res)=>{
  //1:获取参数
  var did=parseInt(req.query.did) ;    //商品id
  let uid=parseInt(req.query.uid);    //获取用户id
  //let uid = 1;
  //console.log(did,req.session.uid);
  //console.log(uid,typeof uid);
  //2:sql select
  var sql=`SELECT id FROM yhc_cart WHERE did=? AND uid=?`;
  pool.query(sql,[did,uid],(err,result)=>{
    if(err) throw err;
    if(result.length==0){//如果收藏数据库没有数据插入数据
      var sql=` INSERT INTO yhc_cart
                VALUES(null,${did},${uid})`;
      pool.query(sql,(err,result)=>{
        if(err) throw err;
        if(result.affectedRows>0){
          res.send({code:1,msg:"添加成功"});
        }else{
          res.send({code:-1,msg:"添加失败"});
        }
      })
    }else{
      res.send({code:2})  //当code为2时表示已经收藏成功
    }
  })
})
module.exports=router;